package com.example.todolist.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.todolist.dto.business.PlanStatusCountDTO;
import com.example.todolist.mapper.TeamPerMapper;
import com.example.todolist.model.TeamFarDO;
import com.example.todolist.mapper.TeamFarMapper;
import com.example.todolist.model.TeamPerDO;
import com.example.todolist.response.BaseResponse;
import com.example.todolist.response.SystemResponse;
import com.example.todolist.service.TeamFarService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.todolist.util.RandowId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author cqq
 * @since 2022-03-21
 */
@Service
public class TeamFarServiceImpl extends ServiceImpl<TeamFarMapper, TeamFarDO> implements TeamFarService {
    @Resource
    private TeamFarMapper teamFarMapper;
    @Resource
    private TeamPerMapper teamPerMapper;
    @Override
    public BaseResponse addTeam(TeamFarDO teamFarDO,String code) {
        int i = this.baseMapper.insert(
                TeamFarDO.builder()
                        .name(teamFarDO.getName())
                        .memo(teamFarDO.getMemo())
                        .uid(teamFarDO.getUid())
                        .code(RandowId.genRandomNum())
                        .teamId(code)
                        .uname(teamFarDO.getUname())
                        .uurl(teamFarDO.getUurl())
                        .status(teamFarDO.getStatus())
                        .content(teamFarDO.getContent())
                        .sort(teamFarDO.getSort())
                        .createDate(teamFarDO.getCreateDate())
                        .build()

        );
        if (i == 1) {
            return SystemResponse.success();
        } else {
            return SystemResponse.fail();
        }
    }

    @Override
    public TeamFarDO joinTeam(TeamFarDO teamFarDO) {

        QueryWrapper<TeamFarDO> wrapper = new QueryWrapper<>();
        wrapper.eq("code", teamFarDO.getCode());

        return this.baseMapper.selectOne(wrapper);
    }

    @Override
    public List<TeamFarDO> selectAll() {
       return  this.baseMapper.selectList(null);

    }

    @Override
    public TeamFarDO selectDetails(String ID) {
        QueryWrapper<TeamFarDO> teamFarDOQueryWrapper = new QueryWrapper<>();
        teamFarDOQueryWrapper.eq("id", ID);
        return this.baseMapper.selectOne(teamFarDOQueryWrapper);
    }

    @Override
    public List<TeamFarDO> selectByOneTeam(String openID) {
        QueryWrapper<TeamFarDO> teamFarDOQueryWrapper = new QueryWrapper<>();
        teamFarDOQueryWrapper.eq("uid", openID);
        return this.baseMapper.selectList(teamFarDOQueryWrapper);
    }

    @Override
    public int deleteTeam(String id) {
        QueryWrapper<TeamFarDO> teamFarDOQueryWrapper = new QueryWrapper<>();
        teamFarDOQueryWrapper.eq("id", id);
        TeamFarDO teamFarDO = this.baseMapper.selectOne(teamFarDOQueryWrapper);
        QueryWrapper<TeamPerDO> teamPerDOQueryWrapper = new QueryWrapper<>();
        teamPerDOQueryWrapper.eq("team_id", teamFarDO.getTeamId());
        teamPerMapper.delete(teamPerDOQueryWrapper);
        return this.baseMapper.delete(teamFarDOQueryWrapper);
    }

    @Override
    public TeamFarDO selectDetailsByTeamID(String teamID) {
        QueryWrapper<TeamFarDO> teamFarDOQueryWrapper = new QueryWrapper<>();
        teamFarDOQueryWrapper.eq("team_id", teamID);
        return this.baseMapper.selectOne(teamFarDOQueryWrapper);
    }

    @Override
    public int updateTeam(TeamFarDO teamFarDO) {
        return this.baseMapper.updateById(teamFarDO);
    }

    @Override
    public List<PlanStatusCountDTO> selectStatusNumbers(String uid) {
        List<PlanStatusCountDTO> planStatusCountDTOS = new ArrayList<>();
        for (int i = 0; i < 2; i++) {
            PlanStatusCountDTO planStatusCount = new PlanStatusCountDTO();
            PlanStatusCountDTO planStatusCountDTO = teamFarMapper.selectStatusNumbers(uid, String.valueOf(i));
            if (planStatusCountDTO == null) {
                planStatusCount.setStatus(String.valueOf(i));
                planStatusCount.setNumber("0");
            } else {
                planStatusCount = planStatusCountDTO;
            }
            planStatusCountDTOS.add(planStatusCount);
        }
        return planStatusCountDTOS;

    }


}
